<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>信息管理</el-breadcrumb-item>
      <el-breadcrumb-item>学员信息</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 搜索与添加区域 -->
      <el-row :gutter="20">
        <el-col :span="7">
          <el-input placeholder="请输入用户名" v-model="queryInfo.username" clearable>
          </el-input>
        </el-col>
        <el-col :span="5">
          <el-input placeholder="请输入姓名" v-model="queryInfo.name" clearable>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button icon="el-icon-search" @click="getMessage">搜索</el-button>
        </el-col>
      </el-row>
    </el-card>

    <el-card>
      <!-- 用户列表区域 -->
      <el-table :data="studentMessageList" border stripe>
        <el-table-column type="index" fixed></el-table-column>
        <el-table-column label="用户id" prop="id" v-if="false"></el-table-column>
        <el-table-column label="用户名" prop="username" width="200px"></el-table-column>
        <el-table-column label="邮箱" prop="email" width="200px"></el-table-column>
        <el-table-column label="电话" prop="phoneNumber" width="120px"></el-table-column>
        <el-table-column label="姓名" prop="name" width="150px" fixed></el-table-column>
        <el-table-column label="性别" prop="sex" width="80px"></el-table-column>
        <el-table-column label="国籍" prop="nationality" width="100px"></el-table-column>
        <el-table-column label="地址" prop="address" width="200px"></el-table-column>
        <el-table-column label="出生日期" prop="dateOfBirth" width="100px"></el-table-column>
        <el-table-column label="身份证号" prop="identityCard" width="180px"></el-table-column>
        <el-table-column label="科目一剩余课时" prop="subjectOneTime" width="120px"></el-table-column>
        <el-table-column label="科目二剩余课时" prop="subjectTwoTime" width="120px"></el-table-column>
        <el-table-column label="科目三剩余课时" prop="subjectThreeTime" width="120px"></el-table-column>
        <el-table-column label="科目四剩余课时" prop="subjectFourTime" width="120px"></el-table-column>
        <el-table-column label="科目一剩余考试次数" prop="subjectOneExam" width="150px"></el-table-column>
        <el-table-column label="科目二剩余考试次数" prop="subjectTwoExam" width="150px"></el-table-column>
        <el-table-column label="科目三剩余考试次数" prop="subjectThreeExam" width="150px"></el-table-column>
        <el-table-column label="科目四剩余考试次数" prop="subjectFourExam" width="150px"></el-table-column>

        <el-table-column label="操作" fixed="right" width="70px">
          <template slot-scope="scope">
            <!--修改信息按钮 -->
            <el-tooltip effect="dark" content="修改信息" placement="top" :enterable="false">
              <el-button type="primary" icon="el-icon-edit" size="mini" @click="updateMessageDialog(scope.row)"></el-button>
            </el-tooltip>

          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                     :current-page="queryInfo.page" :page-sizes="[5, 10, 20, 50]" :page-size="queryInfo.size"
                     layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </el-card>


    <!-- 完善信息的对话框 -->
    <el-dialog title="完善信息" :visible.sync="updateMessageDialogVisible" width="60%">
      <!-- 内容主体区域 -->
      <el-form :model="newMessage" :rules="messageRules" ref="addUserFormRef" label-width="140px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="newMessage.username" placeholder="请输入用户名" disabled></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="newMessage.email" placeholder="请输入邮箱"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="phoneNumber">
          <el-input v-model="newMessage.phoneNumber" placeholder="请输入电话"></el-input>
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="newMessage.name" placeholder="请输入姓名"></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-radio v-model="newMessage.sex" v-for="item in sexOptions" :key="item"
                    :label="item">{{ item }}
          </el-radio>
        </el-form-item>
        <el-form-item label="国籍" prop="nationality">
          <el-input v-model="newMessage.nationality" disabled></el-input>
        </el-form-item>
        <el-form-item label="住址" prop="address">
          <el-input v-model="newMessage.address" placeholder="请输入住址"></el-input>
        </el-form-item>
        <el-form-item label="出生日期" prop="dateOfBirth">
          <el-date-picker v-model="newMessage.dateOfBirth" type="date" value-format="yyyy-MM-dd"
                          placeholder="选择日期"></el-date-picker>
        </el-form-item>
        <el-form-item label="身份证" prop="identityCard">
          <el-input v-model="newMessage.identityCard" placeholder="请输入身份证号"></el-input>
        </el-form-item>
        <el-form-item label="科目一剩余课时" >
          <el-input-number v-model="newMessage.subjectOneTime" :min="0" :max="10000" placeholder="请输入科目一剩余课时"></el-input-number>
        </el-form-item>
        <el-form-item label="科目二剩余课时" >
          <el-input-number v-model="newMessage.subjectTwoTime" :min="0" :max="10000" placeholder="请输入科目二剩余课时"></el-input-number>
        </el-form-item>
        <el-form-item label="科目三剩余课时" >
          <el-input-number v-model="newMessage.subjectThreeTime" :min="0" :max="10000" placeholder="请输入科目三剩余课时"></el-input-number>
        </el-form-item>
        <el-form-item label="科目四剩余课时" >
          <el-input-number v-model="newMessage.subjectFourTime" :min="0" :max="10000" placeholder="请输入科目四剩余课时"></el-input-number>
        </el-form-item>
        <el-form-item label="科目一剩余考试次数" >
          <el-input-number v-model="newMessage.subjectOneExam" :min="0" :max="100" placeholder="请输入科目一剩余考试次数"></el-input-number>
        </el-form-item>
        <el-form-item label="科目二剩余考试次数" >
          <el-input-number v-model="newMessage.subjectTwoExam" :min="0" :max="100" placeholder="请输入科目二剩余考试次数"></el-input-number>
        </el-form-item>
        <el-form-item label="科目三剩余考试次数" >
          <el-input-number v-model="newMessage.subjectThreeExam" :min="0" :max="100" placeholder="请输入科目三剩余考试次数"></el-input-number>
        </el-form-item>
        <el-form-item label="科目四剩余考试次数" >
          <el-input-number v-model="newMessage.subjectFourExam" :min="0" :max="100" placeholder="请输入科目四剩余考试次数"></el-input-number>
        </el-form-item>

      </el-form>
      <!-- 底部区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancelUpdateMessageDialog">取 消</el-button>
        <el-button type="primary" @click="updateMessage">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "MessageStudent",
  data(){
    // 验证邮箱的规则
    var checkEmail = (rule, value, cb) => {
      // 验证邮箱的正则表达式
      const regEmail = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/

      if (regEmail.test(value)) {
        // 合法的邮箱
        return cb()
      }

      cb(new Error('请输入合法的邮箱'))
    }

    // 验证手机号的规则
    var checkPhoneNumber = (rule, value, cb) => {
      // 验证手机号的正则表达式
      const regPhoneNumber = /^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/

      if (regPhoneNumber.test(value)) {
        return cb()
      }

      cb(new Error('请输入合法的手机号'))
    }

    // 校验身份证号
    var checkIdentityCard = (rule, value, cb) => {
        // 验证身份证号的正则表达式
        const regIdentityCard = /^[1-9]\d{5}(18|19|20|(3\d))\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/

        if (regIdentityCard.test(value)) {
          return cb()
        }

        cb(new Error('请输入合法的身份证号'))
      }
    return{
      queryInfo:{
        page: 1,
        size: 10,
        username: '',
        name: '',
        status: ''
      },
      // 学员信息
      studentMessageList: [],
      total: '',
      newMessage: {},
      updateMessageDialogVisible: false,
      sexOptions: ['男', '女'],
      messageRules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {
            min: 3,
            max: 16,
            message: '用户名的长度在3~16个字符之间',
            trigger: 'blur'
          }
        ],
        email: [
          {required: true, message: '请输入邮箱', trigger: 'blur'},
          {validator: checkEmail, trigger: 'blur'}
        ],
        phoneNumber: [
          {required: true, message: '请输入手机号', trigger: 'blur'},
          {validator: checkPhoneNumber, trigger: 'blur'}
        ],
        name: [
          {required: true, message: '请输入姓名', trigger: 'blur'},
          {
            min: 2,
            max: 14,
            message: '用户名的长度在2~14个字符之间',
            trigger: 'blur'
          }
        ],
        sex:[
          {required: true, message: '请选择性别', trigger: 'blur'},
        ],
        address: [
          {required: true, message: '请输入地址', trigger: 'blur'},
        ],
        dateOfBirth: [
          {required: true, message: '请输入出生日期', trigger: 'blur'},
        ],
        identityCard: [
          {required: true, message: '请输入身份证号', trigger: 'blur'},
          {validator: checkIdentityCard, trigger: 'blur'}
        ]
      },
    }
  },
  created() {
    this.getMessage()
  },
  methods:{
    // 获取学员列表
    getMessage(){
      this.$http.post('/driver/student/allStudent', this.queryInfo).then(res => {
        if (res.data.code === 200) {
          this.studentMessageList = res.data.data.studentMessageList
          this.total = res.data.data.total
        } else {
          this.$message.error(res.data.message)
        }
      }).catch(err => {
        console.log(err)
        this.$message.error(err.message)
      })
    },

    // 监听 size 改变的事件
    handleSizeChange(size) {
      this.queryInfo.size = size
      this.getMessage()
    },
    // 监听 page 改变的事件
    handleCurrentChange(page) {
      this.queryInfo.page = page
      this.getMessage()
    },

    cancelUpdateMessageDialog(){
      this.updateMessageDialogVisible = false
    },

    updateMessageDialog(date){
      this.newMessage = {...date}
      this.updateMessageDialogVisible = true
    },
    // 修改学员信息
    updateMessage(){
      this.$http.post('/driver/student/update/student', this.newMessage).then(res => {
        if (res.data.code === 200) {
          this.$message.success(res.data.message)
          this.updateMessageDialogVisible = false
          this.getMessage()
        } else {
          this.$message.error(res.data.message)
        }
      }).catch(err => {
        console.log(err)
        this.$message.error(err.message)
      })
    }
  }
}
</script>

<style scoped>

</style>
